package com.like.update

import android.content.Context
import com.like.update.controller.DownloadController
import com.like.update.downloader.IDownloader
import com.like.update.shower.IShower
import java.io.File

/**
 * 文件下载路径：
 * context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
 * /storage/emulated/(0/1/...)/Android/data/packageName/files/Download
 */
object Update {
    private val mDownloadController by lazy {
        DownloadController()
    }

    /**
     *
     * @param shower        显示者。
     * 库中提供了：[com.like.update.shower.ForceUpdateDialogShower]、[com.like.update.shower.NotificationShower]
     */
    fun shower(shower: IShower): Update {
        mDownloadController.mShower = shower
        return this
    }

    /**
     *
     * @param downloader    下载工具类。
     * 库中提供了：[com.like.update.downloader.RetrofitDownloader]
     */
    fun downloader(downloader: IDownloader): Update {
        mDownloadController.mDownloader = downloader
        return this
    }

    /**
     *
     * @param url           下载地址。必须设置。可以是完整路径或者子路径
     * @param downloadFile  下载的文件。默认是根据url和versionName来创建。
     */
    fun download(
        context: Context,
        url: String,
        downloadFile: File?
    ) {
        mDownloadController.apply {
            this.context = context
            this.mUrl = url
            this.mDownloadFile = downloadFile
            this.cont()
        }
    }

    fun cancel() {
        mDownloadController.cancel()
    }

}